package com.woniu.shop.rabibit;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

/**
 * 消息可靠性保证
 */
@Component
public class RabbitConfirm implements
        RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnCallback {
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        System.out.println("correlationData = " + correlationData);
        System.out.println("ack = " + ack); //false
        System.out.println("cause = " + cause);
        if (ack) {
            System.out.println("消息发送到交换机确认成功！");
        } else {
            System.out.println("消息发送到交换机失败，接下来根据业务做处理，比如：重发");
        }
    }
    @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
        // 消息无法路由到队列时候，再这里可以获取消息内容
        System.out.println("=======消息回退：ReturnCallback=======");
        System.out.println("消息内容 = " + new String(message.getBody()));
        System.out.println("响应编码 = " + replyCode);
        System.out.println("消息无法路由的原因 = " + replyText);
        System.out.println("交换机 = " + exchange);
        System.out.println("路由key = " + routingKey);
    }
}
